package com.fingard.dsp.bank.directbank.hrrz01;

import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.directbank.hrbb01.HRBB01Header;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsHisBalDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

public class HRRZ01Balance extends HRRZ01Base implements IBalance {


    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();
        try {
            //发银行报文的XML对象
            XmlTextWriter xmlWriter = new XmlTextWriter();
            HRRZ01Header header = new HRRZ01Header();
            header.bsnCode = "41004020";
            header.msgType = "1001";
            header.serialNo = p_req7506.reqHeader.transSeqID;
            header.signature = "";
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeStartElement("appBody");
            xmlWriter.writeElementString("queryType","1");
            while (p_req7506.readRecord()) {
                xmlWriter.writeElementString("custAccount", p_req7506.readedDetail.actNum);
            }
            xmlWriter.writeElementString("beginNum", "0");
            xmlWriter.writeElementString("queryNum", "20");
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),header.bsnCode);
            if (tmpStrRet[0].length() == 0) {
                if (getRet(tmpStrRet[1])) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRets[2], tmpSbMsg);
                    if (tmpBkReader != null) {
                        //判断头部状态码

                        String respondCode = tmpRets[0];
                        String respondInfo = tmpBkReader.getFirstText("/resRoot/appHdr/resMsg");
                        if(respondCode.equals("00000000")) {
                            while (tmpBkReader.read("/resRoot/appBody/custInfoList/array/row")) {
                                RetAtsBalDetail tmpRetDetail = new RetAtsBalDetail();
                                tmpRetDetail.respCode = "0";
                                tmpRetDetail.actNum = tmpBkReader.readedXElement.getSingleText("custAccount");
                                String curType = tmpBkReader.readedXElement.getSingleText("currencySymbol");
                                tmpRetDetail.actCur = getBankConfig().getMapKeyByValue("curActMapping", curType);
                                tmpRetDetail.currBal = tmpBkReader.readedXElement.getSingleText("accountBalance");
                                tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("useBalance");
                                retAts.writeRetDetail(tmpRetDetail, p_req7506.isBatch);
                            }
                        }else{
                            retAts.writeRespCode("-1");
                            retAts.writeRespInfo(respondInfo);
                        }
                    } else {
                        retAts.writeRespCode("-1");
                        retAts.writeRespInfo(tmpSbMsg.toString());
                    }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRets[1]);
                }
            } else {
                retAts.writeRespCode("-1");
                retAts.writeRespInfo(tmpStrRet[1]);
            }
        } catch (Exception e) {
            retAts.writeRespCode("-1");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        RetAts7505HisBalQry retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
        String tmpRespCode = "";
        String tmpRespInfo = "";
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            HRRZ01Header header = new HRRZ01Header();
            header.msgType = "1001";
            header.bsnCode = "41004102";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader(xmlWriter, header);
            xmlWriter.writeElementString("accountNo", p_req7505.actNum);
            xmlWriter.writeElementString("currencyType", convertCurToBank(p_req7505.actCur));
            xmlWriter.writeElementString("beginDate", p_req7505.dateFrom);
            xmlWriter.writeElementString("endDate", p_req7505.dateTo);
            xmlWriter.writeElementString("beginNum", "0");
            xmlWriter.writeElementString("queryNum", "20");
            xmlWriter.writeStartElement("inputList");
            xmlWriter.writeStartElement("array");
            XElement tmpXElement = new XElement("row", "");
            tmpXElement.addChild(new XElement("accountNo", p_req7505.actNum));
            tmpXElement.addChild(new XElement("currencyType", convertCurToBank(p_req7505.actCur)));
            xmlWriter.writeXElement(tmpXElement);
            xmlWriter.writeEndDocument();
            String[] tmpRetBkStr = sendToBank(xmlWriter.toXmlString(),header.bsnCode);
            if (tmpRetBkStr[0].length() == 0) {
                if (getRet(tmpRetBkStr[1])) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetBkStr[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String respondCode = tmpRets[0];
                    String respondInfo = tmpBkReader.getFirstText("/resRoot/appHdr/resMsg");
                    WriteBankLogLn("返回状态码为：" + respondCode);
                    if(respondCode.equals("00000000")) {
                        while (tmpBkReader.read("/resRoot/appBody/outputList/array/row")) {
                            RetAtsHisBalDetail tmpRetDetail = new RetAtsHisBalDetail();
                            String tmpETYDAT = tmpBkReader.readedXElement.getSingleText("accountDate");
                            if (tmpETYDAT.length() == 8) {
                                tmpRetDetail.balDate = tmpETYDAT.substring(0, 4) + "-" + tmpETYDAT.substring(4, 6) + "-" + tmpETYDAT.substring(6, 8);
                            } else {
                                tmpRetDetail.balDate = tmpETYDAT;
                            }
                            tmpRetDetail.availBal = tmpBkReader.readedXElement.getSingleText("useBalance");
                            tmpRetDetail.actBal = StringHelper.amountDivide100(tmpBkReader.readedXElement.getSingleText("balance"));
                            retAts.writeRetDetail(tmpRetDetail);
                        }
                    }else{
                        tmpRespCode = "-1";
                        tmpRespInfo = respondInfo;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                 }
                } else {
                    retAts.writeRespCode("-1");
                    retAts.writeRespInfo(tmpRets[1]);
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpRetBkStr[1];
            }

        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        if (tmpRespCode.equals("-1")) {
            retAts = new RetAts7505HisBalQry(p_req7505.reqHeader);
            retAts.writeStart();//写返回ATS的统一头部信息
            retAts.writeRespCode(tmpRespCode);
            retAts.writeRespInfo(tmpRespInfo);
        } else if (retAts.getWrittenRdCount() == 0) {
            retAts.writeRespCode("0");
            retAts.writeRespInfo("返回结果为空，所查询时间段内不存在历史余额");
        }
        retAts.writeEnd();
        return retAts;
    }
}
